package annotation;

import org.springframework.context.annotation.ComponentScan;

import java.lang.reflect.Method;

/**
 * <p>
 *  自定义注解
 * </p>
 *
 * @author LovelyBigDuck
 * @date 2021/7/5 18:59
 */
@ComponentScan("annotation.*")
public class Test {
    @CherryAnnotation(name = "fangXK",age = 23,score = {99,66,77})
    public static void study(int times){
        for(int i = 0; i < times; i++){
            System.out.println("Good Good Study, Day Day Up!");
        }
    }

    @ServiceLog
    public static void testLog() throws Exception {
        Thread.sleep(1000);
    }

    public static void main(String[] args) throws Exception {
        study(3);
        // 使用注解
        Class<?> testClass = Class.forName("annotation.Test");
        Method studyMethod = testClass.getMethod("study", int.class);
        // 如果该方法被标注了该注解
        if(studyMethod.isAnnotationPresent(CherryAnnotation.class)){
            System.out.println("Test类上配置了CherryAnnotation");
            // 获取该元素上指定类型的注解
            CherryAnnotation cherryAnnotation = studyMethod.getAnnotation(CherryAnnotation.class);
            // 打印出注解的内容
            System.out.println("name: " + cherryAnnotation.name() + ", age: " + cherryAnnotation.age()
                    + ", score: " + cherryAnnotation.score()[0]);
        }
        else {
            System.out.println("Test类上没有配置注解");
        }

        // 测试切入点日志方法计时
        testLog();
    }
}
